/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import App.Facada;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author Jaso
 */
public class LoginPanel extends Panel implements ActionListener{
    private Button btnLogin;
    private Label lblId;
    private Label lblPin;
    private TextField txtId;
    private TextField txtPin;
    
    private GUI gui;
    
    private String accountName;
    private String pinNr;
    private Label lblErrorMsg;
    private int width;
    private Facada facada;


    public LoginPanel(Facada facada) {
        width = 700;
        this.facada = facada;
        initComponent(); 
    }
    
    public void initComponent()	{
        this.setBackground(new Color (143, 188, 143));
        this.setSize(width,500);
        this.setLayout(null);
        
        lblErrorMsg = new Label();
        lblErrorMsg.setFont(new Font("Serif", Font.PLAIN, 25));
        lblErrorMsg.setForeground(new Color(255, 0, 0));
        lblErrorMsg.setBounds(width/2-275, 150, 550  , 50);
        add(lblErrorMsg);
        
        
        lblId = new Label("Account Name:");
        lblId.setFont(new Font("Serif", Font.PLAIN, 20));
        lblId.setBounds(width/2 - 200 , 200, 160  , 30);
        add(lblId);
        
        txtId = new TextField();
        txtId.setFont(new Font("Serif", Font.PLAIN, 20));
        txtId.setBounds(width/2 - 30 , 200, 200  , 30);
        add(txtId);
        
        lblId = new Label("Pin Number:");
        lblId.setFont(new Font("Serif", Font.PLAIN, 20));
        lblId.setBounds(width/2 -200, 250, 160  , 30);
        add(lblId);
        
        txtPin = new TextField();
        txtPin.setFont(new Font("Serif", Font.PLAIN, 20));
        txtPin.setBounds(width/2 -30, 250, 200  , 30);
        add(txtPin);
        
        
        btnLogin = new Button("Log in");
        btnLogin.setBackground(new Color(189, 183, 107));
        btnLogin.addActionListener(this);
        btnLogin.setFont(new Font("Serif", Font.PLAIN, 20));
        btnLogin.setBounds(width  - 150, 350, 70  , 30);

        add(btnLogin);
        
        
    }

    public void clearTextFields()   {
        txtId.setText(null);
        txtPin.setText(null);
    }
    
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if(isValidInput()  )    {
            if(e.getSource() == btnLogin)   {
                gui.SwitchPanels("account");
            }
        } else  {
            setErrorMsg();
        }
    }
    
    public void setErrorMsg()   {
        lblErrorMsg.setText("Please enter a valid Account Number & Pin Number");
        
        double start = System.currentTimeMillis();
        
        while(System.currentTimeMillis() - start < 1500){
            lblErrorMsg.setVisible(true);
        }
        lblErrorMsg.setVisible(false);
    }

    public boolean isValidInput()   {
        
            setPinNr(txtPin.getText());
            setAccountName(txtId.getText());
            return facada.confirmLoginDetails();

    }
    
    /**
     * @return the pinNr
     */
    public String getPinNr() {
        return pinNr;
    }


    /**
     * @return the kontoNr
     */
    public String getAccountName() {
        return accountName;
    }

    /**
     * @param kontoNr the kontoNr to set
     */
    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    /**
     * @param pinNr the pinNr to set
     */
    public void setPinNr(String pinNr) {
        this.pinNr = pinNr;
    }

    /**
     * @param gui the gui to set
     */
    public void setGui(GUI gui) {
        this.gui = gui;
    }

}
 
